package cn.bgs.service.Impl;

import cn.bgs.dto.JwtManagerDto;
import cn.bgs.mapper.UserMapper;
import cn.bgs.req.LoginReq;
import cn.bgs.res.Result;
import cn.bgs.service.ManagerService;
import cn.bgs.util.JwtUtils;
import cn.bgs.vo.JwtManagerVo;
import cn.bgs.vo.Managervo;
import cn.bgs.vo.loginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class ManagerServiceImpl implements ManagerService {

    @Resource
    private UserMapper managerMapper;
    @Override
    public Result<loginVo> login(@RequestBody LoginReq loginReq) {
        //查询用户是否存在

       loginVo managervo =  managerMapper.login(loginReq);
       if(managervo == null){
           return Result.buildFailure("用户名或密码不正确");
       }else {
           loginVo vo= generateToken(managervo);
           return Result.buildSuccess(vo);
       }
    }

    private loginVo generateToken(loginVo managervo){
        loginVo dto = new loginVo();
        BeanUtils.copyProperties(managervo,dto);
       String token= JwtUtils.getToken(dto);
       //返回给前端token+用户信息
        loginVo vo=new loginVo();
        BeanUtils.copyProperties(dto,vo);
        vo.setToken(token);
        return vo;
    }
    @Override
    public int updateUser(loginVo user) {
        return managerMapper.updateUser(user);
    }

    @Override
    public int updateOperat(loginVo user) {
        return managerMapper.updateOperat(user);
    }
}
